/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package networkowner;

import contractmessageservice.ContractMessage;
import contractmessageservice.PartyType;
import electricitysupplier.ResponseMessage;
import java.util.Date;

/**
 *
 * @author ool
 */
public class MessageValidatorService {

    Esit1no1Dao dao;

    public MessageValidatorService() {
        dao = new Esit1no1Dao();

    }

    public boolean isClientInDb(String clientRegNum) {
        return dao.isClientInDb(clientRegNum);
    }

    public boolean isProviderInDb(String providerRegNum) {
        return dao.isProviderInDb(providerRegNum);
    }

    public boolean clientHasContract(String clientRegNum, Date contractStartDate) {
        return dao.clientHasContract(clientRegNum, contractStartDate);
    }

    public void validateContractMessage(ContractMessage contractMessage, ResponseMessage resp) {
        boolean result = true;
        StringBuilder message = new StringBuilder();
        ContractMessage.ClientParty clientParty = contractMessage.getClientParty();
        PartyType providerParty = contractMessage.getProviderParty();
        ContractMessage.ContractInfo contractInfo = contractMessage.getContractInfo();

        if (isClientInDb(clientParty.getRegistryCode())) {
            result = false;
            message.append("Client not in db. ");
        }
        if (isProviderInDb(providerParty.getRegistryCode())) {
            result = false;
            message.append("Crovider not in db. ");
        }
        if (!clientHasContract(clientParty.getRegistryCode(), contractInfo.getStartDate().toGregorianCalendar().getTime())) {
            result = false;
            message.append("Client already has a contract. ");
        }
        resp.setAccepted(result);
        if (!result) {
            resp.setMessage(message.toString());
        }
    }
}
